package com.hwpt.hwaccount.feature.personalinfo;

import com.hwpt.hwaccount.core.data.local.entity.UserEntity;

import java.lang.ref.WeakReference;

public class PersonalInfoPresenter implements PersonalInfoContract.Presenter {
    private WeakReference<PersonalInfoContract.View> viewRef;
    private final PersonalInfoContract.Model model;
    private UserEntity currentUser;

    public PersonalInfoPresenter(PersonalInfoContract.Model model) {
        this.model = model;
    }

    @Override
    public void attachView(PersonalInfoContract.View view) {
        this.viewRef = new WeakReference<>(view);
    }

    @Override
    public void detachView() {
        if (viewRef != null) {
            viewRef.clear();
        }
    }

    @Override
    public void onDestroy() {
        if (model instanceof PersonalInfoModel) {
            ((PersonalInfoModel) model).dispose();
        }
        detachView();
    }


    public void checkRealnameInfo() {
        model.getUserInfo(new PersonalInfoContract.Model.DataCallback<UserEntity>() {
            @Override
            public void onSuccess(UserEntity data) {
                currentUser = data;
                PersonalInfoContract.View view = viewRef.get();
                if (view != null) {
                    // 检查实名认证信息是否完整
                    if (data.getRealName() != null && !data.getRealName().isEmpty() &&
                            data.getIdCard() != null && !data.getIdCard().isEmpty()) {
                        // 实名信息完整，显示脱敏信息
                        String maskedRealName = maskRealName(data.getRealName());
                        String maskedIdCard = maskIdCard(data.getIdCard());
                        view.showRealnameInfo(maskedRealName, maskedIdCard);
                    } else {
                        // 实名信息不完整，弹出认证对话框
                        view.showRealnameInputDialog();
                    }
                }
            }

            @Override
            public void onError(String message) {
                PersonalInfoContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("加载失败: " + message);
                }
            }
        });
    }

    // 实名信息脱敏方法
    private String maskRealName(String realName) {
        if (realName == null || realName.length() <= 1) {
            return realName;
        }
        // 显示最后一个字，其他用*代替
        int length = realName.length();
        return "*" + realName.substring(length - 1);
    }

    private String maskIdCard(String idCard) {
        if (idCard == null || idCard.length() < 18) {
            return idCard;
        }
        // 显示第一位和最后一位，中间用*代替
        return idCard.substring(0, 1) + "****************" + idCard.substring(17);
    }

    @Override
    public void loadUserInfo() {
        PersonalInfoContract.View view = viewRef.get();
        model.getUserInfo(new PersonalInfoContract.Model.DataCallback<UserEntity>() {
            @Override
            public void onSuccess(UserEntity data) {
                currentUser = data;
                PersonalInfoContract.View view = viewRef.get();
                if (view != null) {
                    view.showUserInfo(data);
                }
            }

            @Override
            public void onError(String message) {
                PersonalInfoContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("加载失败: " + message);
                }
            }
        });
    }

    @Override
    public void updateNickname(String nickname) {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        currentUser.setNickname(nickname);
        updateUserInfo();
    }

    @Override
    public void updateGender(int gender) {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        currentUser.setGender(gender);
        updateUserInfo();
    }

    @Override
    public void updateBirthday(String birthday) {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        currentUser.setBirthday(birthday);
        updateUserInfo();
    }

    @Override
    public void updateRegion(String region) {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        currentUser.setCountryRegion(region);
        updateUserInfo();
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        PersonalInfoContract.View view = viewRef.get();

        model.updateAvatar(currentUser.getHanWangId(), avatarUrl, new PersonalInfoContract.Model.DataCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                currentUser.setAvatarUrl(avatarUrl);
                PersonalInfoContract.View view = viewRef.get();
                if (view != null) {
                    view.showSuccess("头像更新成功");
                    view.showUserInfo(currentUser);
                }
            }

            @Override
            public void onError(String message) {
                PersonalInfoContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("头像更新失败: " + message);
                }
            }
        });
    }

    @Override
    public void doRealNameAuth(String realName, String idCard) {
        if (currentUser == null) {
            showError("请先加载用户信息");
            return;
        }

        currentUser.setRealName(realName);
        currentUser.setIdCard(idCard);
        updateUserInfo();
    }

    private void updateUserInfo() {
        PersonalInfoContract.View view = viewRef.get();


        model.updateUserInfo(currentUser, new PersonalInfoContract.Model.DataCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                PersonalInfoContract.View view = viewRef.get();
                if (view != null) {
                    view.showSuccess("更新成功");
                    view.showUserInfo(currentUser);
                }
            }

            @Override
            public void onError(String message) {
                PersonalInfoContract.View view = viewRef.get();
                if (view != null) {
                    view.showError("更新失败: " + message);
                }
            }
        });
    }

    private void showError(String message) {
        PersonalInfoContract.View view = viewRef.get();
        if (view != null) {
            view.showError(message);
        }
    }
}